﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace 重载构造函数
{
    class Time
    {
        private int hour;
        private int minute;
        private int second;

        public Time(int hh, int mm, int ss)
        {
            SetTime(hh, mm, ss);
        }//end three-parameter constructor

        //no-argument constructor:initialize each instance variable to zero
        public Time() : this(0, 0, 0) { }

        //Time constructor: hour supplied, minute and second defaulted to 0
        public Time(int hh) : this(hh, 0, 0) { }

        //Time constructor:hour and minute supplied, second defaulted to 0
        public Time(int hh, int mm) : this(hh, mm, 0) { }

        //Time constructor: another Time object supplied 
        public Time(Time time1) : this(time1.Hour, time1.Minute, time1.Second) { }

        //set a new time using universal time 
        public void SetTime(int hh, int mm, int ss)
        {
            Hour = hh;
            Minute = mm;
            Second = ss;
        }//end method SetTime

        public int Hour
        {
            set
            {
                hour = (value>=0 && value<24) ? value : 0;
            }//end set
            get
            {
                return hour;
            }//end get
        }//end property Hour 

        public int Minute
        {
            get
            {
                return minute;
            }//end get
            set
            {
                minute = (value >= 0 && value < 60) ? value : 0;
            }//end set
        }//end property Minute

        public int Second
        {
            get
            {
                return second;
            }//end get
            set
            {
                second = (value >= 0 && value < 60) ? value : 0;
            }//end set
        }//end property Second 

        public string ToUniversalString()
        {
            return string.Format("{0:D2}:{1:D2}:{2:D2}", Hour, Minute, Second);
        }//end method ToUniversalString

        public override string ToString()
        {
            return string.Format("{0}:{1:D2}:{2:D2} {3}", ((0 == Hour || 12 == Hour) ? 12 : (Hour % 12)), Minute,
                Second, (Hour < 12) ? "AM" : "PM");
        }//end method ToString 

        public void Display(string header)
        {
            Console.WriteLine(header);
            Console.WriteLine("  {0}", this.ToUniversalString());
            Console.WriteLine("  {0}", this.ToString());
            Console.WriteLine();
        }//end method Display
    }//end class time
}
